package com.example.springbootdemo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.springbootdemo.bean.core.AnyConnectionManager;
import com.example.springbootdemo.bean.core.HubConnectionManager;
import com.example.springbootdemo.bean.kernelApi.LoginData;
import com.example.springbootdemo.bean.kernelApi.Result;
import com.example.springbootdemo.bean.kernelApi.Sort;
import com.example.springbootdemo.bean.kernelApi.ThingSearchReq;
import com.example.springbootdemo.dict.comparefiledict.AnalysisStatus;
import com.example.springbootdemo.dto.comparefile.ResultData;
import com.example.springbootdemo.dto.comparefile.ThingHistoryFileObj;
import com.example.springbootdemo.dto.createWorkInstance.CreateWorkInstanceRequest;
import com.example.springbootdemo.dto.createWorkInstance.DataDetail;
import com.example.springbootdemo.dto.createWorkInstance.DataItem;
import com.example.springbootdemo.dto.createWorkInstance.RootData;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.signalr.HubConnection;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class HistoryServiceImpl implements HistoryService {



    @Autowired
    AnyConnectionManager anyConnectionManager;

    @Autowired
    HubConnectionManager hubConnectionManager;


    @Override
    public ResultData getAJNHistoryList() {
        var type = new Result<ThingHistoryFileObj>();
        var search = new ThingSearchReq();
/*        search.setSkip((1 - 1) * 10);
        search.setTake(10);*/
        search.getSort().add(new Sort("T478992412400816128", true));

        //过滤筛选
/*        LinkedList<String> strings = new LinkedList<>();
        strings.add("T478992412400816128");
        strings.add("=");
        strings.add("S478992497914286080");
        search.setFilter(strings);*/

        //通过属性筛选
        ArrayList<Object> userData = new ArrayList<>();
        userData.add("T478992412400816128");
        search.setUserData(userData);
        HubConnection anyHubConnection = anyConnectionManager.getHubConnection();
        anyConnectionManager.tokenAuthConnection();
//        hubConnection.invoke("TokenAuth", "1223244").blockingAwait();
        var  result = anyHubConnection.invoke(type.getClass(),"Load", "445723536485650432", search).blockingGet();

        var json = JSON.toJSONString(result);;
        ResultData result1 = JSON.parseObject(json, ResultData.class);

        return result1;

    }

    @Override
    public Result createHistoryToAJN(DataDetail dataDetail) {

        CreateWorkInstanceRequest createWorkInstanceRequest = initData();

        createWorkInstanceRequest.getParams().setData(initHisDataJson(dataDetail));

        var type2 = new Result<LoginData>();
        Result request = hubConnectionManager.getHubConnection().invoke(type2.getClass(), "Request", createWorkInstanceRequest).blockingGet();

        return request;
    }


    /**
     * 初始化实际业务数据
     * @return
     */
    private String initHisDataJson(DataDetail dataDetail){
        ObjectMapper objectMapper = new ObjectMapper();
        RootData createWorkInstanceRequest = null;
        try {
            createWorkInstanceRequest = objectMapper.readValue(new ClassPathResource("orign.json").getFile(), RootData.class);


            //json after里面的内容
            List<DataDetail> dataDetails = new ArrayList<>();

 /*           DataDetail dataDetail = new DataDetail();
            //主键id，设置了之后就会更新
//            dataDetail.setId("27741198015972352");
            dataDetail.setFileName("测试7");
            dataDetail.setFileContent("啊啊啊啊\n" +
                    "安安静静\n" +
                    "啊卡机卡机\n" +
                    "\n" +
                    "拉大距离五十块\n" +
                    "的理解哇撒旦记录卡\n" +
                    "\n" +
                    "1记录接口记录");
            dataDetail.setFileId("8aada46b889f5c820188aea72fdc1eba");
            dataDetail.setFilePath("/opt");
            dataDetail.setName("historyFile");*/
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String createTimeString = simpleDateFormat.format(date);
            dataDetail.setCreateDate(createTimeString);
            dataDetail.setStatus("正常");
            dataDetail.setReState(AnalysisStatus.SUCCESS.getCode());

            dataDetails.add(dataDetail);


/*            DataDetail dataDetail1 = new DataDetail();
            dataDetail1.setFileName("测试4");
            dataDetails.add(dataDetail1);*/


            List<DataItem> dataItems = createWorkInstanceRequest.getData().get_478992906363998208();
            DataItem dataItem = dataItems.get(0);
//            dataItem.setCreateTime(createTimeString);
            dataItem.setAfter(dataDetails);
        } catch (IOException e) {
            log.error("json转换异常");
            throw new RuntimeException(e);
        }

        String jsonString = JSONObject.toJSONString(createWorkInstanceRequest);
        return jsonString;
    }
    /**
     * 初始化最外层数据
     * @return
     */
    private  CreateWorkInstanceRequest initData() {

        ObjectMapper objectMapper = new ObjectMapper();
        CreateWorkInstanceRequest createWorkInstanceRequest = null;
        try {
            createWorkInstanceRequest = objectMapper.readValue(new ClassPathResource("work-instance.json").getFile(), CreateWorkInstanceRequest.class);
        } catch (IOException e) {
            log.error("json转换异常");
            throw new RuntimeException(e);
        }

        return createWorkInstanceRequest;

    }


}
